package cn.yhjz.platform.system.controller;

import cn.yhjz.common.AjaxResponse;
import cn.yhjz.common.util.IdUtil;
import cn.yhjz.platform.system.service.ISysRoleService;
import cn.yhjz.platform.system.util.LoginUtil;
import cn.yhjz.platform.system.entity.SysRole;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 系统角色表 前端控制器
 * </p>
 *
 * @author maguoping
 * @since 2022-08-09
 */
@RestController
@RequestMapping("/system/sysRole")
@PreAuthorize("hasAuthority('system:role')")
public class SysRoleController {

    @Autowired
    private ISysRoleService sysRoleService;

    @RequestMapping("/list")
    public AjaxResponse list(@RequestBody SysRole sysRole) {
        List<SysRole> sysRoleList = sysRoleService.list();
        return AjaxResponse.success(sysRoleList);
    }

    /**
     * 新增
     *
     * @param sysRole
     * @return
     */
    @PostMapping("saveOrUpdate")
    public AjaxResponse saveOrUpdate(@RequestBody SysRole sysRole) {
        if (sysRole.getId() == null) {
            sysRole.setRoleCode(IdUtil.generate());
            sysRole.setCreatedBy(LoginUtil.getCurrentUser().getUserCode());
            sysRole.setCreatedTime(new Date());
        } else {
            sysRole.setUpdatedBy(LoginUtil.getCurrentUser().getUserCode());
            sysRole.setUpdatedTime(new Date());
        }
        boolean res = sysRoleService.saveOrUpdate(sysRole);
        if (res) {
            return AjaxResponse.success("添加用户成功");
        } else {
            return AjaxResponse.fail("添加用户失败");
        }
    }
}
